﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PJ.Business.Partner;
using PJ.Business.Inventory;
using PJ.Framework.Common;
using System.Collections.ObjectModel;
using PJ.Data.SalePurchase;

namespace PJ.Business.SalePurchase
{
    public abstract class ShoppingCart<OD> : EntityExtension<OD>, IShoppingCart
        where OD : IShoppingCartData
    {
        //private IShoppingCartItemService m_ItemsService;
        //private IEnumerable<string> m_Items;

        public ShoppingCart(OD data)
            : base(data)//, IShoppingCartService ownerService)
        {
            //if (itemsService == null)
            //    throw new ArgumentNullException();
            //m_ItemsService = itemsService;
        }
        public ShoppingCart()//IShoppingCartService ownerService, IShoppingCartItemService itemsService)
            //: base(ownerService)
        {
            //if (itemsService == null)
            //    throw new ArgumentNullException();
            //m_ItemsService = itemsService;
        }

        //public override string Id
        //{
        //    get
        //    {
        //        return base.Id;
        //    }
        //    set
        //    {
        //        if (value != Id)
        //        {
        //            base.Id = value;
        //            m_Items = null;
        //        }
        //    }
        //}

        public string Reference
        {
            get
            {

                return ObjectData.Reference;
            }
            set
            {
                if (ObjectData.Reference != value)
                {
                    ObjectData.Reference = value;
                }
            }
        }


        //public IEnumerable<string> ShoppingCartItems
        //{
        //    get
        //    {
        //        if (m_Items == null && string.IsNullOrEmpty(Id))
        //            m_Items = m_ItemsService.CachedFindFragments(this.Id);
        //        return m_Items;
        //    }
        //}
    }
    /*
        private string m_ID;
        private IEntity m_Owner;
        private IEntityService m_OwnerService;
        private IShoppingCartItemService m_ShoppingCartItemService;
        private ObservableCollection<IEntityFragment> m_ShoppingCartItems;

        public ShoppingCart(IEntityService ownerService, IShoppingCartItemService cartItemsService)
        {
            if (ownerService == null || cartItemsService == null)
                throw new ArgumentNullException();
            m_OwnerService = ownerService;
            m_ShoppingCartItemService = cartItemsService;
        }

        public IEntityService OwnerService
        {
            get { return m_OwnerService; }
        }

        public IShoppingCartItemService ShoppingCartItemService
        {
            get { return m_ShoppingCartItemService; }
        }

        public string Id
        {
            get
            {
                return m_ID;
            }
            set
            {
                if (value != m_ID)
                {
                    OnIDChanging();
                    m_ID = value;
                    OnIDChange();
                    if (m_Owner != null)
                        if (m_Owner.Id != m_ID)
                            Owner = OwnerService.FindByID(m_ID);
                    if (m_ShoppingCartItems != null)
                        ShoppingCartItems = ShoppingCartItemService.GetFragments(m_ID);
                }
            }
        }
        protected virtual void OnIDChange()
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new System.ComponentModel.PropertyChangedEventArgs("Id"));
        }
        protected virtual void OnIDChanging()
        {
        }

        public IEntity Owner
        {
            get
            {
                if (string.IsNullOrEmpty(m_ID))
                    return null;
                if (m_Owner == null)
                {
                    Owner = OwnerService.FindByID(m_ID);
                }
                return m_Owner;
            }
            set
            {
                if (m_Owner != value)
                {
                    if (value == null)
                        throw new ArgumentNullException();
                    OnOwnerChanging();
                    OnGlobalIDChanging();
                    m_Owner = value;
                    OnGlobalIDChange();
                    OnOwnerChange();
                    if (m_Owner.Id != m_ID)
                        Id = m_Owner.Id;
                }
            }
        }

        protected virtual void OnOwnerChange()
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new System.ComponentModel.PropertyChangedEventArgs("Owner"));
        }
        protected virtual void OnOwnerChanging()
        {
        }

        public string GlobalID
        {
            get { return Owner.GlobalID; }
        }
        protected virtual void OnGlobalIDChange()
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new System.ComponentModel.PropertyChangedEventArgs("GlobalID"));
        }
        protected virtual void OnGlobalIDChanging()
        {
        }
        public ObservableCollection<IEntityFragment> ShoppingCartItems
        {
            get
            {
                if (m_ShoppingCartItems == null)
                    if (!string.IsNullOrEmpty(m_ID))
                        ShoppingCartItems = ShoppingCartItemService.GetFragments(m_ID);
                return m_ShoppingCartItems;
            }
            set
            {
                if (value != m_ShoppingCartItems)
                {
                    if (value == null)
                        throw new ArgumentNullException();
                    OnShoppingCartItemsChanging();
                    m_ShoppingCartItems = value;
                    OnShoppingCartItemsChange();
                }
            }
        }
        protected virtual void OnShoppingCartItemsChange()
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new System.ComponentModel.PropertyChangedEventArgs("ShoppingCartItems"));
        }
        protected virtual void OnShoppingCartItemsChanging()
        {
        }


        public void CopyFrom(IBusinessObject data)
        {
            throw new NotImplementedException();
        }

        public void InitData(object data)
        {
            throw new NotImplementedException();
        }


        public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;

        public virtual void Dispose()
        {
        }

        public virtual string Error
        {
            get { return null; }
        }

        public virtual string this[string columnName]
        {
            get { return null; }
        }


    }*/
}
